package com.huidz.calendar.activity;

import android.app.Activity;
import android.app.ActivityOptions;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.transition.Slide;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.huidz.calendar.R;
import com.huidz.calendar.ViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class GoodayActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager home_pager;
    private List<View> fraglist;
    //
//    //ViewPager适配器
    private ViewPagerAdapter pagerAdapter;
    //底部导航栏的四个rela
    /*****************************/
    private TabLayout mytab;
    private ImageView comeback;
    //共享给自己的fragment的数据
//    public static String
    private RelativeLayout title_ji_rela, title_yi_rela;
    private View title_yi_h, title_ji_h;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Slide slide = new Slide(Gravity.RIGHT);
        slide.setDuration(200L);//间歇时间
        getWindow().setEnterTransition(slide);
        setContentView(R.layout.activity_arrangements);
        inti();
        AddFragment(savedInstanceState);
    }

    /**
     * 初始化加载调用方法
     */
    public void inti() {
        home_pager = findViewById(R.id.home_pager);
        comeback = findViewById(R.id.title_comeback);
        comeback.setOnClickListener(this);
        mytab = findViewById(R.id.mytab);
        mytab.addTab(mytab.newTab().setText("宜"));
        mytab.addTab(mytab.newTab().setText("忌"));
        mytab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                // 开启Fragment事务
                if (tab.getText().equals("宜")) {
                    home_pager.setCurrentItem(0);
                }
                if (tab.getText().equals("忌")) {
                    home_pager.setCurrentItem(1);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            mytab.getTabAt(position).select();
//            Seleced(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }

    private Intent intentMain, intentCircle;
    private LocalActivityManager manager;

    /**
     * 用于绑定fragment
     */
    public void AddFragment(Bundle savedInstanceState) {
        //添加frag到集合中
        fraglist = new ArrayList<>();
//        fraglist.add(new NiecdayFragment());
//        fraglist.add(new LowdayFragment());
        manager = new LocalActivityManager(this, true);
        manager.dispatchCreate(savedInstanceState);
        intentMain = new Intent(GoodayActivity.this, NicedayActivity.class);
        View tab01 = manager.startActivity("viewID", intentMain).getDecorView();
        intentCircle = new Intent(GoodayActivity.this, LowdayActivity.class);
        View tab02 = manager.startActivity("viewID", intentCircle).getDecorView();
        fraglist.add(tab01);//将页面添加到View集合  
        fraglist.add(tab02);
        home_pager.setOnPageChangeListener(new MyPagerChangeListener());
        //绑定适配器
//        pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fraglist);
        home_pager.setAdapter(pagerAdapters);
        home_pager.setCurrentItem(0);
        home_pager.setOffscreenPageLimit(2);
    }

    //绑定点击事件
    PagerAdapter pagerAdapters = new PagerAdapter() {
        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return fraglist.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            // TODO Auto-generated method stub
            container.removeView(fraglist.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub
            container.addView(fraglist.get(position));


            return fraglist.get(position);
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_comeback:
                onBackPressed();
                break;
//            case R.id.title_ji_rela:
//                home_pager.setCurrentItem(0);
//                Seleced(1);
//
//                break;
//            case R.id.title_yi_rela:
//                home_pager.setCurrentItem(1);
//                Seleced(0);
//                break;
        }
    }
//    public void Seleced(int i){
//        if(i==0){
//            title_yi_h.setVisibility(View.INVISIBLE);
//            title_ji_h.setVisibility(View.GONE);
//        }else{
//            title_yi_h.setVisibility(View.GONE);
//            title_ji_h.setVisibility(View.INVISIBLE);
//        }
//    }

    public static long firstTime = 0;
    public static String strs="";
    public static void intactivity(Activity activity,String str){
        Intent toxdw = new Intent(activity, YiJiLookActivity.class);
        toxdw.putExtra("title", str);
        activity.startActivity(toxdw, ActivityOptions.makeSceneTransitionAnimation(activity).toBundle());
    }


}
